package com.hxs.learn.plugins.plugins.gen;

import org.beetl.core.Template;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.core.kit.StringKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class MapperCodeGen implements CodeGen {
    private static Logger logger = LoggerFactory.getLogger(MapperCodeGen.class);
    String CR = System.getProperty("line.separator");
    String pkg = null;

    public MapperCodeGen() {

    }

    public MapperCodeGen(String basepkg, String moduleName) {
        this.pkg = basepkg + ".mapper";
        if (null != moduleName && moduleName.trim().length() > 0) {
            this.pkg = basepkg + ".mapper." + moduleName;
        }

    }

    public static String mapperTemplate = "";

    static {
//		mapperTemplate = GenConfig.getTemplate("/org/icec/gen/core/mapper.btl");
        mapperTemplate = GenConfig.getTemplate("/sql/mapper.btl");
    }

    @Override
    public void genCode(String project, String entityPkg, String entityClass, TableDesc tableDesc, GenConfig config,
                        boolean isDisplay) {
        if (pkg == null) {
            pkg = entityPkg;
        }
        Template template = SourceGen.gt.getTemplate(mapperTemplate);
        String mapperClass = entityClass + "Mapper";
        template.binding("className", mapperClass);
        template.binding("package", pkg);
        template.binding("entityClass", entityClass);
        template.binding("entityClassName", StringKit.toLowerCaseFirstOne(entityClass));

        String mapperHead = "import " + entityPkg + "." + entityClass + ";" + CR;
        template.binding("imports", mapperHead);
        String mapperCode = template.render();
        if (isDisplay) {
            System.out.println();
            System.out.println(mapperCode);
        } else {
            try {
                SourceGen.saveSourceFile(project, pkg, mapperClass, mapperCode);
            } catch (IOException e) {
                throw new RuntimeException("mapper代码生成失败", e);
            }
        }
        logger.info("mapper succeed！");
    }

}
